//
//  CEHttpController.swift
//  CJPack
//
//  Created by 陈警卫 on 2024-12-10.
//

import UIKit

class CEHttpController: CJBaseBackController {
    
    var tableView: UITableView = UITableView(frame: .zero, style: .plain)
    var data: [[String: String]] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupInit()
        setupView()
        setupData()
    }
}

extension CEHttpController {
    
    func setupInit() {
        CJHttpManager.shared.baseUrl = "https://ebike-api-dev.vinkacloud.com/"
        CJHttpManager.shared.basePoint = "api"
        
        CEHttper().login(userName: "chenjw", password: "123456") { config, model in
        }
        
        CEHttper().loginTest(userName: "chenjw", password: "123456") { config in
        }
    }
    
    func httpRefreshToken() {
        CEHttper().refreshTest { config in
            self.tableView.cj_mj_headerEndRefreshing()
        }
    }
    
    func httpUserInfo() {
        CEHttper().userInfo { config, model in
            
        }
        CEHttper().userInfo { config, model in
            
        }
        CEHttper().userInfo { config, model in
            
        }
    }
    
}

extension CEHttpController {
    
    func setupView() {
        tableView.frame = .init(x: 0,
                                y: CJScreen.navBatBarHeight,
                                width: view.cj_width,
                                height: CJScreen.safeHeight)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        view.addSubview(tableView)
        
        tableView.cj_mj_headerRefresh { [weak self] scrollView in
            self?.httpRefreshToken()
        }
    }
    
    func setupData() {
        data = [
            ["name": "下拉刷新Token"],
            ["name": "userInfo"],
            ["name": "Download"]
        ]
        tableView.reloadData()
    }
    
}

extension CEHttpController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        cell.backgroundColor = .clear
        cell.selectionStyle = .none
        
        let param = data[indexPath.row]
        let name = param["name"]
        
        cell.textLabel?.text = name
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let param = data[indexPath.row]
        let name = param["name"]
        if name == "userInfo" {
            httpUserInfo()
        } else if name == "Download" {
            downloadAction()
        }
    }
    
}

extension CEHttpController {
    
    func downloadAction() {
        
        CEHttper().down()
        
    }
    
}
